package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.SalaryBasedata;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.ISalaryBasedataService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/salaryDasedataController", produces = MediaType.APPLICATION_JSON_VALUE)
public class SalaryDasedataController extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryDasedataController.class);


    @Autowired
    private ISalaryBasedataService service;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    /**
     * 查询 工资项基础数据分页
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/querySalaryBasedataPage")
    public ApiResultDTO querySalaryBasedataPage(@RequestHeader("X-Token") String tokenid,
                                                @RequestParam(value = "page") String page,
                                                  @RequestParam(value = "limit") String limit) {
        try {
            String province = getUser(tokenid).getProvince();
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<SalaryBasedata> pageDTO = service.querySalaryBasedataPage(paginationDTO,province);
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }

    /**
     * 新增 工资项基础数据
     * @param salaryBasedata
     * @return
     */
    @PostMapping("/saveSalaryBasedataPage")
    public ApiResultDTO saveSalaryBasedataPage(@RequestHeader("X-Token") String tokenid,
                                               @RequestBody SalaryBasedata salaryBasedata) {
        try {
            String province = getUser(tokenid).getProvince();
            service.saveSalaryBasedata(salaryBasedata,province);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 工资项基础数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 工资项基础数据发生异常");
        }
    }

    /**
     * 更新 工资项基础数据
     * @param salaryBasedata
     * @return
     */
    @PostMapping("/updateSalaryBasedataPage")
    public ApiResultDTO updateSalaryBasedataPage(@RequestBody SalaryBasedata salaryBasedata) {
        try {
            service.updateSalaryBasedata(salaryBasedata);
            return ApiResultDTO.success(null, "更新成功");
        } catch (Exception e) {
            logger.error("更新 工资项基础数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "更新 工资项基础数据发生异常");
        }
    }

    /**
     * 保存 工资项基础数据
     * @param salaryBasedataList
     * @return
     */
    @PostMapping("/submitSalaryBasedataPage")
    public ApiResultDTO submitSalaryBasedataPage(@RequestHeader("X-Token") String tokenid,
                                                 @RequestBody List<SalaryBasedata> salaryBasedataList) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资配置管理--工资项目维护--保存");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            String province = getUser(tokenid).getProvince();
            service.submitSalaryBasedata(salaryBasedataList,province);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("保存 工资项目维护数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    /**
     * 删除 工资项基础数据
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteSalaryBasedata/{ids}")
    public ApiResultDTO deleteSalaryBasedata(@PathVariable("ids") String ids) {
        try {
            service.deleteSalaryBasedata(ids);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("删除 工资项目维护数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 工资项目维护数据发生异常");
        }
    }

    /**
     * 根据条件查询 工资项基础数据
     * @return
     */
    @GetMapping("/querySalaryBasedataInfo")
    public ApiResultDTO querySalaryBasedataInfo(@RequestHeader("X-Token") String tokenid) {
        try {
            String province = getUser(tokenid).getProvince();
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("province" , province);
            List<SalaryBasedata> list = service.querySalaryBaseDataInfo(queryParam);
            return ApiResultDTO.success(list,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }
    /**
     * 根据条件查询 工资项基础数据
     * @return
     */
    @GetMapping("/queryBgtSalaryBaseDataInfo")
    public ApiResultDTO queryBgtSalaryBaseDataInfo(@RequestHeader("X-Token") String tokenid) {
        try {
            String province = getUser(tokenid).getProvince();
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("province" , province);
            List<SalaryBasedata> list = service.queryBgtSalaryBaseDataInfo(queryParam);
            return ApiResultDTO.success(list,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }
    /**
     * 查询标准项
     * @param tokenid
     * @return
     */
    @GetMapping("/queryStandardBasedataInfo")
    public ApiResultDTO queryStandardBasedataInfo(@RequestHeader("X-Token") String tokenid) {
        try {
            String province = getUser(tokenid).getProvince();
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("province" , province);
            List<SalaryBasedata> list = service.queryStandardBasedataInfo(queryParam);
            return ApiResultDTO.success(list,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }
}
